<?php

// +----------------------------------------------------------------------
// | H1CMS © OpenSource CMS
// +----------------------------------------------------------------------
// | Copyright (c) 2014-2016 http://www.h1cms.com All rights reserved.
// | Copyright (c) 2014-2016 嘉兴领格信息技术有限公司，并保留所有权利。
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Allen <allen@lg4.cn>
// +----------------------------------------------------------------------

namespace h1cms\model;

use h1cms\system\Model;

/**
 * Node Language
 * 
 * @property int $node_id Node ID
 * @property int $lang_id Language ID
 *
 * @author Allen <allen@lg4.cn>
 */
class NodeLanguage extends Model {

    public $table = 'node_language';
    public $primaryKey = 'node_id';
    public $languageKey = 'lang_id';

    /**
     * 
     * @param int $node_id
     * @param int $lang_id
     * @return \h1cms\model\NodeLanguage
     */
    public static function get($node_id, $lang_id = 0) {
        return static::getDb()
                        ->query("select * from {node_language} where node_id=:node_id AND lang_id=:lang_id",
                                [
                            'node_id' => $node_id,
                            'lang_id' => $lang_id
                        ])
                        ->fetchObject(get_called_class());
    }

    /**
     * 
     * @param int $node_id
     * @return \h1cms\model\NodeLanguage
     */
    public static function getAll($node_id) {
        $languages = static::findAll(['node_id' => $node_id]);
        $data = [];
        foreach ($languages as $lang) {
            $data[$lang->lang_id] = $lang;
        }
        return $data;
    }

}
